<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>图书馆里</el-breadcrumb-item>
      <el-breadcrumb-item>图书审核</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getbookList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getbookList()"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
    </el-card>
    <!-- 用户列表(表格)区域 -->
    <el-table :data="bookList" border stripe>
      <el-table-column type="index" label="索引"></el-table-column>
      <el-table-column label="图书名称" prop="book_name"></el-table-column>
      <el-table-column label="图书封面">
        <template slot-scope="scope">
          <img
            style="width:80px;height:120px;border:none;"
            :src="baseURL + scope.row.book_cover"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column
        label="图书分类"
        prop="book_category_id"
      ></el-table-column>
      <el-table-column label="图书作者" prop="book_author"></el-table-column>
      <el-table-column label="图书出版社" prop="book_press"></el-table-column>
      <el-table-column
        label="图书原价"
        prop="book_price_yuanjia"
      ></el-table-column>
      <el-table-column
        label="图书售价"
        prop="book_price_xianjia"
      ></el-table-column>
      <el-table-column label="备注" prop="book_remarks"></el-table-column>
      <el-table-column label="库存量" prop="book_num"></el-table-column>
      <el-table-column label="发布人" prop="book_seller"></el-table-column>
      <el-table-column label="发布时间" prop="book_date"></el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <!-- 审核通过 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="审核通过"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="warning"
              icon="el-icon-s-check"
              size="mini"
              @click="setBookExamine(scope.row.book_id)"
            ></el-button>
          </el-tooltip>
          <!-- 审核不通过 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="审核不通过"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="danger"
              icon="el-icon-s-check"
              size="mini"
              @click="setBookBackExamine(scope.row.book_id)"
            ></el-button>
          </el-tooltip>
          <!-- 删除 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeBookById(scope.row.book_id)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页导航区域
            @size-change(pagesize改变时触发)
            @current-change(页码发生改变时触发)
            :current-page(设置当前页码)
            :page-size(设置每页的数据条数)
            :total(设置总页数) -->
    <!-- <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination> -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 搜索框绑定数据
      queryInfo: {
        query: '',
        book_examine: 0,
        pagenum: 1, // 当前的页数
        pagesize: 6 // 当前每页显示多少条数据
      },
      baseURL: 'http://127.0.0.1:3000/',
      // 总页数
      total: 3,
      // 图书信息
      bookList: []
    }
  },
  created() {
    this.getbookList()
  },
  methods: {
    // 获取图书列表
    async getbookList() {
      const { data: res } = await this.$http.get('book/getAllBooks', {
        params: this.queryInfo
      })
      if (res.status !== 200) {
        return this.$message.error('获取图书列表失败')
      }
      console.log(res.data)
      this.bookList = res.data
    },
    // 审核图书
    async setBookExamine(bookId) {
      const confirmResult = await this.$confirm('确定上架该图书?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消审核')
      const { data: res } = await this.$http.put('book/examineBook', {
        book_examine: 1,
        book_id: bookId
      })
      if (res.code !== 200) return this.$message.error('审核失败')
      this.$message.success('审核成功')
      this.getbookList()
    },
    async setBookBackExamine(bookId) {
      const confirmResult = await this.$confirm(
        '确定该图书不予审核通过?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消审核')
      const { data: res } = await this.$http.put('book/examineBook', {
        book_examine: 3,
        book_id: bookId
      })
      if (res.code !== 200) return this.$message.error('审核失败')
      this.$message.success('审核成功')
      this.getbookList()
    },
    // 删除图书
    async removeBookById(bookId) {
      const confirmResult = await this.$confirm('确定删除该图书?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      // 如果用户点击确认，则confirmResult 为'confirm'
      // 如果用户点击取消, 则confirmResult获取的就是catch的错误消息'cancel'
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      // 发送请求根据id完成删除操作
      const { data: res } = await this.$http.post('book/deleteBook', {
        book_id: bookId
      })
      // 判断如果删除失败，就做提示
      if (res.status !== 200) return this.$message.error('删除图书失败')
      // 修改成功的提示
      this.$message.success('删除图书成功')
      // 重新请求最新的数据
      this.getbookList()
    },
    // 页数改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
    },
    // 页码改变
    handleCurrentChange(current) {
      this.queryInfo.pagenum = current
    }
  }
}
</script>

<style></style>
